//这个是服务器的自动化构建
import gulp from 'gulp';
import gulpif from 'gulp-if';
import livereload from 'gulp-livereload';
import gls from 'gulp-live-server';
import args from './util/args';

gulp.task('serve', function(cb) {
    //看参数里是否含有watch的监视参数
    if(!args.watch){
        return cb();
    }

    //2. run script with cwd args, e.g. the harmony flag
    const server = gls.new(['--harmony', 'server/bin/www']);
    //this will achieve `node --harmony myapp.js`
    //you can access cwd args in `myapp.js` via `process.argv`
    server.start();

    //use gulp.watch to trigger server actions(notify, start or stop)
    gulp.watch(['server/public/**/*.js', 'server/views/**/*.ejs'], function (file) {
        //这个是通知服务器哪些文件改变了
        server.notify.apply(server, [file]);
    });

    // Note: try wrapping in a function if getting an error like `TypeError: Bad argument at TypeError (native) at ChildProcess.spawn`
    gulp.watch(['server/routes/**/*.js','server/app.js'], function() {
        //重启服务器,如果服务器文件改变了话
        server.start.bind(server)()
    });
});












